<?php

$f3=require('../lib/base.php');

$f3->set('DEBUG',3);
$f3->set('UI','../views/');
$f3->config('../setup.cfg');
$f3->route('GET /',
           function($f3) {
               $param = $f3->get('PARAMS');
               $page  = str_replace("/?page=",'',$param[0]);
               $host  = '';
               $home  = '';
               $pages = '';

               if (isset($page) && $page >0) 
                   $host = 'http://ks.cn.yahoo.com/dir/0_over.html?page='.$page;
               else
                   $host = 'http://ks.cn.yahoo.com/dir/0_over.html';

               $req = \Web::instance()->request($host);
               if (isset($req['body'])) {
                   $raw = $req['body'];
                   $raw = preg_replace('/[\n\r\t]/i','',$raw);
                   $raw = str_replace("'",'"',$raw);
                   
                   preg_match('/<table.*?<\/table>/i',$raw, $home);
                   preg_match('/<div class="pageover".*?<\/div>/i', $raw, $pages);

                   $home = $home[0];
                   $home = preg_replace('/\/question\/([0-9]*)\.html/i','/questions/q$1.html',$home);
                   $home = preg_replace('/\/dir\/([0-9]*)_ask\.html/i','/categories/c$1.html',$home);
                   $home = preg_replace('/\/browse\/get_class\.php\?fatherid=([0-9]*)/i','/categories/c$1.html',$home);
                   $home = str_replace('cellspacing="0"','class="table table-striped"',$home);
                   $home = str_replace('爱问iAsk','YEP8网',$home);
                   $home = preg_replace('/<td class="gray">[0-9]*<\/td>/i','',$home);
                   $home = str_replace('<th>回答</th>','',$home);
                   $home = str_replace('<tr><td></td></tr>','',$home);

                   $pages = str_replace('pageover','pagination',$pages[0]);
                   $pages = preg_replace('/<li class="current">([0-9]*)<\/li>/i','<li class="active"><a href="javascript:void(0);">$1</a></li>',$pages) ;    
               
               }
               $f3->set('pages',$pages);
               $f3->set('home', $home);
               $f3->set("css", "home.css");
               echo View::instance()->render('home.html');
           },0//24*3600
    );

$f3->route('GET /categories/*', 
           function ($f3) {
               $params = $f3->get("PARAMS");
               $url = $params[1];
               $this_link = $url;
               $pages = explode("?",$this_link);
               $page = "page=0";
               if (count($pages)>1)
                   $page = $pages[1] ;
              
               $url = preg_replace('/^c([0-9]*)\.html(.*)$/i',"$1",$url);
               $url = "http://ks.cn.yahoo.com/dir/".$url."_over.html?".$page;

               $breadcrumbs = "";
               $category    = "";
               $list        = "";
               $pages       = "";

               $req = \Web::instance()->request($url);
               if (isset($req['body'])) {
                   $raw = $req['body'];
                   $raw = preg_replace('/[\n\r\t]/i','',$raw);

                   preg_match('/<title>.*?<\/title>/i', $raw, $title);
                   preg_match('/<meta content="[\s\S]*?" name="keywords">/i', $raw, $keywords);
                   if(isset($title))
                       $title = str_replace(array("雅虎知识堂"),array("问答网"),$title);

                   if ($keywords != null)
                       $keywords = str_replace(array("雅虎知识堂",">"),array("问答网","/>"),$keywords);

                   $raw = preg_replace('/\/question\/([0-9]*)\.html/i','/questions/q$1.html',$raw);
                   $raw = preg_replace('/\/dir\/([0-9]*)_ask\.html/i','/categories/c$1.html',$raw);

                   $f3->set("site_name", $title[0]);
                   $f3->set("site_keywords", $keywords[0]);

                   $category = '<div id="categories" class="well"><ul class="nav nav-pills">';
                   
                   preg_match('/<div class="other"><div>[\s\S]*?<\/div><\/div>/i',$raw,$tmp);
                   if (isset($tmp[0])) {
                       $temp = preg_match_all('/<a[\s\S]*?<\/a>/i',$tmp[0], $as);
                       foreach($as[0] as $a) {
                           $category .= "<li>" .$a . '</li>';
                       }
                   }
                   $category .= "</ul></div>";
                  
                   preg_match('/<div class="way"[\s\S]*?<\/div>/i',$raw, $breadcrumbs);
                   $breadcrumbs = $breadcrumbs[0];


                   $breadcrumbs = str_replace("way", "breadcrumbs",$breadcrumbs);
                   $breadcrumbs = str_replace("/dir/",'/',$breadcrumbs);
                   $breadcrumbs = str_replace('知识分类','问答网',$breadcrumbs);

                   $links = array();
                   $links[0]["href"] = "/";
                   $links[0]["text"] = "首页";

                   preg_match_all('/<a title=".*" href="(.*?)">(.*?)<\/a>/i', $breadcrumbs,$bbs);

                   for($i=0; $i< count($bbs[0]); $i++) {
                       $tl = preg_replace('/\/([0-9]*)_over\.html/i','/categories/c$1.html',$bbs[1][$i]);
                       $links[$i+1]["href"] = $tl;//$bbs[1][$i];
                       $links[$i+1]["text"] = $bbs[2][$i];
                   }
                   

                   preg_match_all('/.*>([\s\S]*?)<\/div>$/i', $breadcrumbs, $lastitem);

                   $idx = count($links);
                   $links[$idx]["href"] = '/categories/'.$this_link;
                   $links[$idx]["text"] = $lastitem[1][0];


                   $breadcrumbs = Web\Url::instance()->generateBreadcrumbs($links);

                   preg_match('/<table.*?<\/table>/i',$raw,$list);

                   $list = str_replace('cellspacing="0"','class="table table-hover"',$list[0]);
                   $list = str_replace("'",'"',$list);
                   $list = preg_replace('/[\n\r\t]/i','',$list);
                   $list = preg_replace('/<td class="gray">[0-9]*<\/td>/i','',$list);
                   $list = str_replace('<th>回答</th>','',$list);

                   preg_match('/<div class="pageover">.*?<\/div>/i',$raw, $tmp);
                   if (isset($tmp[0])) {
                       $pages = $tmp[0];
                       $pages = str_replace('pageover','pagination',$pages);
                       $pages = preg_replace('/<li class="current">([0-9]*)<\/li>/i','<li class="active"><a href="javascript:void(0);">$1</a></li>',$pages) ;    
 
                   }
               }

               $f3->set("breadcrumbs",$breadcrumbs);
               $f3->set("category",$category);
               $f3->set("pages",$pages);
               $f3->set('site_desc', '<meta name="description" content="问答网 - 用户可以搜索其他用户沉淀的精彩内容，浏览到最精彩的问答内容"/>');
               $f3->set("list",$list);
               $f3->set("css", "category.css");
               echo View::instance()->render('category.html');

           },5*3600
    );

$f3->route('GET /questions/*',
           function($f3) {
               $breadcrumbs = '';
               $qtitle = '';
               $qcontent = '';
               $accepted = '暂无';
               $others = '';
               $related = '';

               $params = $f3->get("PARAMS");
               $url = $params[1];
               $this_link = $url;

               $url = preg_replace('/q([0-9]+)\.html(.*)/i',"http://ks.cn.yahoo.com/question/$1.html$2",$url);
               $req = \Web::instance()->request($url);

               if (isset($req['body'])) {

                   $raw = preg_replace('/\n\r\t/i', '',$req['body']);
                   
                   $raw = preg_replace('/\/question\/([0-9]*)\.html/i','/questions/q$1.html',$raw);
                   $raw = preg_replace('/\/dir\/([0-9]*)\.html/i','/categories/c$1.html',$raw);

                   preg_match('/<div class="way-l"[\s\S]*?<\/div>/i',$raw,$breadcrumbs);

                   if ($breadcrumbs) {
                       $breadcrumbs = str_replace("wal-l", "breadcrumbs",$breadcrumbs);
                       $breadcrumbs = $breadcrumbs[0];


                       $breadcrumbs = str_replace("way", "breadcrumbs",$breadcrumbs);
                       $breadcrumbs = str_replace("/dir/",'/',$breadcrumbs);
                       $breadcrumbs = str_replace('知识分类','问答网',$breadcrumbs);

                       $links = array();
                       $links[0]["href"] = "/";
                       $links[0]["text"] = "首页";

                       preg_match_all('/<a title=".*" href="(.*?)">(.*?)<\/a>/i', $breadcrumbs,$bbs);

                       for($i=0; $i< count($bbs[0]); $i++) {
                           $tl = preg_replace('/\/([0-9]*)_over\.html/i','/categories/c$1.html',$bbs[1][$i]);
                           $links[$i+1]["href"] = $tl;//$bbs[1][$i];
                           $links[$i+1]["text"] = $bbs[2][$i];
                       }
                   

                       preg_match_all('/.*>([\s\S]*?)<\/div>$/i', $breadcrumbs, $lastitem);

                       $idx = count($links);
                       $links[$idx]["href"] = '/categories/'.$this_link;
                       $links[$idx]["text"] = $lastitem[1][0];

                       $breadcrumbs = Web\Url::instance()->generateBreadcrumbs($links);
                   
                   } else 
                       $breadcrumbs = '';
                   
                   $f3->set("breadcrumbs",$breadcrumbs);
                   preg_match('/<title>[\s\S]*?<\/title>/i', $raw, $title);
                   $title = str_replace('雅虎知识堂','问答网',$title[0]);
                   preg_match('/<meta content=".*" name="keywords">/i', $raw ,$keywords);
                   if (isset($keywords[0]))
                       $keywords = str_replace(array("雅虎知识堂",">"),array("问答网", "/>"), $keywords[0]);
                   else 
                       $keywords = $f3->get('site_keywords');

                   $description = preg_match('/<meta content=".*" name="description">/i',$raw, $desc);
                   if (isset($desc[0])) {
                       $description = str_replace(array("雅虎知识堂", "雅虎知识问答,",">"),array("问答网","", "/>"), $desc[0]);
                   }

                   $f3->set("site_name", $title);
                   $f3->set("site_desc", $description);
                   $f3->set("site_keywords", $keywords);

                   $h1 = '<p class="lead">网友提问';
                   preg_match('/<h1>((.|\n)*?)<\/h1>/i',$raw,$tmp);

                   if ($tmp != null)
                       $h1 .= ': <a href="' . $this_link .'">'.  $tmp[1] . '</a>';

                   $h1 .='</p>';
                   
                   $questions = "";
                   preg_match_all('/<div class="pb5">[\s\S]*?<\/div>/i',$raw,$h1d);
                   if ($h1d[0] != null) {
                       $h1 .= $h1d[0][0];
                       for ($i=1;$i<count($h1d[0]);$i++) {
                           if(strpos($h1d[0][$i],'提问者对答案的评价')<= 0)
                               $questions .= '<div class="well"><p class="lead">回答者:<a href="http://www.20000.asia">问答网</a></p><blockquote><p>'.$h1d[0][$i]."</p></blockquote></div>";
                       }
 
                   }
                       
                   $f3->set('h1',$h1);

                   $questions = preg_replace('/<script[\s\S]*?<\/script>/i','', $questions);
                   $questions = preg_replace('/[\n\r\t]/i','',$questions);

                   preg_match('/<div class="bd">.*?<\/div>/i',$raw,$tmp);
                   if (isset($tmp[0])) {
                       $related = $tmp[0];
                       $related = str_replace('<ul>', '<ul class="related-ul">', $related);
                       $related = str_replace('/question/','/questions/q',$related);
                       $tmp = null;
                   }

                   preg_match('/<div class="best-answer fixclear con">[\s\S]*?class="fixclear/i',$raw,$tmp);

                   if (isset($tmp[0])) {
                       preg_match('/<pre>[\s\S]*?<\/pre>/i',$tmp[0],$ta);
                       $accepted = $ta[0];
                   }
                   

                   preg_match_all('/<pre>[\s\S]*?<\/pre>/i', $raw,$tmps);
                   if (isset($tmp[0]) && count($tmps[0]) >0) {
                       $others = '<ul class="others-ul">';
                       foreach($tmps[0] as $tmp)
                           $others .="<li>".$tmp."</li>";
                       $others .= "</ul>";
                   }
               }

               $f3->set('questions', $questions);
               $f3->set('css', 'question.css');
               $f3->set('accepted', $accepted);
               $f3->set('others', $others);
               $f3->set('related', $related);
               
               echo View::instance()->render('question.html');
           },5*3600
    );
$f3->run();
